package com.bbcare.extendContent.zdy.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bbcare.comm.AppException;
import com.bbcare.comm.Constants;
import com.bbcare.comm.ErrorCode;
import com.bbcare.comm.ResultModel;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 硬件数据对接
 */
@Controller
@RequestMapping("/webapi/service/hardWare")
public class HardWareDataController {
    @Resource(name = "jedisPool")
    private JedisPool jedisPool;
    // 查询用户信息
    @ResponseBody
    @RequestMapping("/saveData")
     public ResultModel<String> saveData(@RequestBody String arg, HttpServletRequest request)
            throws Exception {
        Jedis js = jedisPool.getResource();
        try {
            JSONObject jsonArg = JSON.parseObject(arg);
            JSONObject jsonData = jsonArg.getJSONObject(Constants.INF_INPUTPARAM_COMMONFIELD);
            String userId = jsonData.getString("userId");
            js.lpush(userId + "_hardWare", jsonData.toJSONString());
            if (js.llen(userId + "_hardWare") > 5) {
                js.rpop(userId + "_hardWare");
            }
        }finally {
            if (null != js) {
                try {
                    js.close();
                } catch (Exception ex) {
                    ex.printStackTrace();
                    throw (new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_REDIS_CONNECT_CLOSE_FAILED,
                            "Redis缓存关闭失败!"));
                }
            }
        }
        return new ResultModel<String>("ok");
    }
    @ResponseBody
    @RequestMapping("/getData")
    public ResultModel<Object>getData(@RequestBody String arg, HttpServletRequest request){
        Jedis js = jedisPool.getResource();
        List<String> result =null;
        try {
        JSONObject jsonArg =  JSON.parseObject(arg);
        JSONObject jsonData = jsonArg.getJSONObject(Constants.INF_INPUTPARAM_COMMONFIELD);
        String userId = jsonData.getString("userId");
         result = js.lrange(userId+"_hardWare",0,5);
        }finally {
            if (null != js) {
                try {
                    js.close();
                } catch (Exception ex) {
                    ex.printStackTrace();
                    throw (new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_REDIS_CONNECT_CLOSE_FAILED,
                            "Redis缓存关闭失败!"));
                }
            }
        }
        return new ResultModel<Object>(result);
    }

    @RequestMapping("/getCookie")
    public ResultModel<Object>getCookie(@RequestBody String arg, HttpServletRequest request){
        Jedis js = jedisPool.getResource();
        String result = null;
        try {
        JSONObject jsonArg =  JSON.parseObject(arg);
        JSONObject jsonData = jsonArg.getJSONObject(Constants.INF_INPUTPARAM_COMMONFIELD);
        String deviceId = jsonData.getString("deviceId");
         result = js.get(deviceId+"_hardWare");
        }finally {
            if (null != js) {
                try {
                    js.close();
                } catch (Exception ex) {
                    ex.printStackTrace();
                    throw (new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_REDIS_CONNECT_CLOSE_FAILED,
                            "Redis缓存关闭失败!"));
                }
            }
        }
        return new ResultModel<Object>(result);
    }

    @ResponseBody
    @RequestMapping("/setCookie")
    public ResultModel<String> setCookie(@RequestBody String arg, HttpServletRequest request)
            throws Exception {
        Jedis js = jedisPool.getResource();
        try {
            JSONObject jsonArg = JSON.parseObject(arg);
            JSONObject jsonData = jsonArg.getJSONObject(Constants.INF_INPUTPARAM_COMMONFIELD);
            String deviceId = jsonData.getString("deviceId");
            js.set(deviceId + "_hardWare", arg);
        }finally {
                if (null != js) {
                    try {
                        js.close();
                    } catch (Exception ex) {
                        ex.printStackTrace();
                        throw (new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_REDIS_CONNECT_CLOSE_FAILED,
                                "Redis缓存关闭失败!"));
                    }
                }
            }
        return new ResultModel<String>("ok");
    }






}
